#include "Justin_Beeper.h"
//注意：单位时间是10ms，加上最高周期是500，单周期支持5s
unsigned char Beeper_Switch[Beeper_Routine];//1--off,0--on
unsigned char Flag_ConstantRing=0;
unsigned char Flag_MuteLock=0;
unsigned char Flag_RingInitFinished=0;//防重初始化标志位，如果需要防重入，在函数里面加多一个if就好了
u16 Beeper_SingleRoutine;
unsigned int Beeper_SwitchCounter;
int Beeper_Repetition,Beeper_RepetitionCounter;

void Beeper_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    Beeper_Mute();
}
void Beeper_Control(void)
{
    if(Flag_MuteLock)
    {
        Beeper_Mute();
        return;
    }
    if(!Beeper_Repetition&&!Flag_ConstantRing)
    {
        Beeper_SwitchCounter=0;
        return;
    }
    if(Beeper_SwitchCounter>Beeper_SingleRoutine-1)
    {
        Beeper_SwitchCounter=0;
        if(Flag_ConstantRing)
            return;
        Beeper_RepetitionCounter++;
        if(Beeper_RepetitionCounter>=Beeper_Repetition)
        {
            Beeper_Mute();
        }
    }
    
    Beep=Beeper_Switch[Beeper_SwitchCounter];
}

//蜂鸣器鸣响指令，参数：鸣叫时间，关闭时间，这样子的周期重复几次
void Beeper_RingCmd(u16 Ring_10ms,u16 Mute_10ms,u16 Repetition)
{
    int i;
    Beeper_Mute();
    Beeper_SingleRoutine=Ring_10ms+Mute_10ms;
    Beeper_Repetition=Repetition;
    for(i=0;i<Ring_10ms;i++)
    {
        Beeper_Switch[i]=0;
    }
    for(i=Ring_10ms;i<Ring_10ms+Mute_10ms;i++)
    {
        Beeper_Switch[i]=1;
    }
}

//蜂鸣器常开指令，参数：指定蜂鸣器的鸣叫和关闭时间
void Beeper_ConstantRingCmd(u16 Ring_10ms,u16 Mute_10ms)
{
    int i;
    Beeper_Mute();
    Beeper_SingleRoutine=Ring_10ms+Mute_10ms;
    Beeper_Repetition=0;
    Flag_ConstantRing=1;
    for(i=0;i<Ring_10ms;i++)
    {
        Beeper_Switch[i]=0;
    }
    for(i=Ring_10ms;i<Ring_10ms+Mute_10ms;i++)
    {
        Beeper_Switch[i]=1;
    }
}

//用于故障告警的工作函数，类似于constantring,但是加入防重入函数，使其可以在不断执行的错误响应函数里面只执行一次。
//同时，加入了routineinterval变量用于实现更丰富的告警信号传递。
//工作周期：（鸣叫-停止）-（）-……（重复指定次数）-（鸣叫-停止）-周期间间隔
//注意：整个工作周期不能超过Beeper_Routine!
void Beeper_Warning(u16 Ring_10ms,u16 Mute_10ms,u16 Repetition,u16 RoutineInterval)
{
    int i=0,j=0,Pointer=0;
    if(Flag_RingInitFinished)
    {
        return;
    }
    Beeper_Mute();
    Beeper_SingleRoutine=(Ring_10ms+Mute_10ms)*Repetition+RoutineInterval;
    Beeper_Repetition=0;
    Flag_ConstantRing=1;
    for(j=0;j<Repetition;j++)
    {
        for(i=0;i<Ring_10ms;i++,Pointer++)
        {
            Beeper_Switch[Pointer]=0;
        }
        for(i=Ring_10ms;i<Ring_10ms+Mute_10ms;i++,Pointer++)
        {
            Beeper_Switch[Pointer]=1;
        }
    }
    for(i=(Ring_10ms+Mute_10ms)*Repetition;i<Beeper_SingleRoutine;i++)
    {
        Beeper_Switch[Pointer]=1;
    }
    Flag_RingInitFinished=1;
}
void Beeper_Mute(void)
{
    int i;
    Beep=1;
    for(i=0;i<Beeper_Routine;i++)
    {
        Beeper_Switch[i]=1;
    }
    Beeper_Repetition=0;
    Beeper_RepetitionCounter=0;
    Beeper_SingleRoutine=0;
    Beeper_SwitchCounter=0;
    Flag_ConstantRing=0;
    Flag_RingInitFinished=0;
}
